package DBNode

import "gopkg.in/mgo.v2-unstable/bson"
import "fmt"

const accountCollection = "Account"

//Account 玩家账号
type Account struct {
	_Id      bson.ObjectId `bson:"_id"`
	DeviceID string
	NickName string
	Level    int
}

func accountQuery(deviceID string) *Account {
	query := db.C(accountCollection).Find(bson.M{"deviceid": deviceID})
	retAccount := &Account{}
	err := query.One(retAccount)
	if err == nil {
		return retAccount
	}
	fmt.Println(err)
	return nil
}

func accountRegister(deviceID string, nickName string) (*Account, error) {
	newAccount := &Account{_Id: bson.NewObjectId(), DeviceID: deviceID, NickName: nickName, Level: 1}
	err := db.C(accountCollection).Insert(newAccount)
	if err != nil {
		return nil, err
	}
	return newAccount, nil
}
